package testplan.servlets;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import testplan.Log;
import testplan.services.UserService;

@Singleton
public class SetMockUserFilter implements Filter 
{
	
	@Inject private UserService userService;

	@Override
	public void destroy() 
	{
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException 
	{
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		if(httpRequest.getSession().getAttribute("user") == null)
		{
			httpRequest.getSession().setAttribute("user", userService.getDevUser());
			Log.ACCESS.info("Mock user is set");
		}
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException 
	{
		assert userService != null;
	}

}
